package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.mapper.TbItemParamMapper;
import com.taotao.pojo.*;
import com.taotao.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 李小磊 on 2017/3/18.
 */
@Service
public class ItemParamServiceImpl  implements ItemParamService{
    @Autowired
    private TbItemParamMapper itemMapper;

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public EasyUIDataGridResult getItemParamList(int page,int rows) {
        //分页
        PageHelper.startPage(page,rows);
        TbItemParamExample itemParamExample = new TbItemParamExample();
        List<TbItemParam> list = itemMapper.selectByExampleWithBLOBs(itemParamExample);
        List<TbItemParamExt> newList = new ArrayList<>();
        for (TbItemParam itemParam:list
             ) {
            TbItemParamExt tbItemParamExt = new TbItemParamExt();
            Long itemCatId = itemParam.getItemCatId();
            TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(itemCatId);
            tbItemParamExt.setId(itemParam.getId());
            tbItemParamExt.setItemCatId(itemParam.getItemCatId());
            tbItemParamExt.setCreated(itemParam.getCreated());
            tbItemParamExt.setUpdated(itemParam.getUpdated());
            tbItemParamExt.setParamData(itemParam.getParamData());
            tbItemParamExt.setName(itemCat.getName());
            newList.add(tbItemParamExt);
        }
        PageInfo<TbItemParamExt> info = new PageInfo<TbItemParamExt>(newList);
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        //取分页数据
        result.setTotal(info.getTotal());
        result.setRows(newList);
        return result;
    }

    @Override
    public TaotaoResult getItemCatByCid(Long cid) {
        TbItemParamExample itemParamExample = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = itemParamExample.createCriteria();
        criteria.andItemCatIdEqualTo(cid);
        List<TbItemParam> list = itemMapper.selectByExampleWithBLOBs(itemParamExample);
        if (list != null && list.size() >0){
            TbItemParam tbItemParam = list.get(0);
            return TaotaoResult.ok(tbItemParam);
        }
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteItemByIds(String ids) {
        String[] strings = ids.split(",");
        for (String id: strings
             ) {
            itemMapper.deleteByPrimaryKey(Long.parseLong(id));
        }
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult saveItemCat(Long cid, String paramData) {
        TbItemParam tbItem = new TbItemParam();
        tbItem.setItemCatId(cid);
        tbItem.setParamData(paramData);
        tbItem.setCreated(new Date());
        tbItem.setUpdated(new Date());
        itemMapper.insert(tbItem);
        return TaotaoResult.ok();
    }
}
